package com.dzk.cp.c11x5.dao.impl.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.dzk.cp.c11x5.dao.R5Dao;
import com.dzk.cp.c11x5.pojo.R5;

public abstract class R5DaoJdbcImpl<T extends R5> extends JdbcDaoSupport implements R5Dao<T> {
	
	@Override
	public void save(T r5) {
		String sql = "insert into " + getTableName() + "(qs, n1, n2, n3, n4, n5, ns, js, os, ds, xs, y0, y1, y2, lx, hz, kd, yl, cm) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		Object[] args = new Object[] {r5.getQs(), r5.getN1(), r5.getN2(), r5.getN3(), 
				r5.getN4(), r5.getN5(), r5.getNs(), r5.getJs(), r5.getOs(), r5.getDs(), r5.getXs(), 
				r5.getY0(), r5.getY1(), r5.getY2(), r5.getLx(), r5.getHz(), r5.getKd(), r5.getYl(), r5.getCm()};
		super.getJdbcTemplate().update(sql, args);
	}
	
	@Override
	public void batchSave(List<T> r5s) {
		String sql = "insert into " + getTableName() + "(qs, n1, n2, n3, n4, n5, ns, js, os, ds, xs, y0, y1, y2, lx, hz, kd, yl, cm) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		for (T r5 : r5s) {
			batchArgs.add(new Object[] {r5.getQs(), r5.getN1(), r5.getN2(), r5.getN3(), 
					r5.getN4(), r5.getN5(), r5.getNs(), r5.getJs(), r5.getOs(), r5.getDs(), r5.getXs(), 
					r5.getY0(), r5.getY1(), r5.getY2(), r5.getLx(), r5.getHz(), r5.getKd(), r5.getYl(), r5.getCm()});
		}
		super.getJdbcTemplate().batchUpdate(sql, batchArgs);
	}
	
	@Override
	public int getCount() {
		String sql = "select count(1) from " + getTableName();
		return super.getJdbcTemplate().queryForInt(sql);
	}
	
	@Override
	public T getLastR5() {
		int lastId = this.getCount();
		if (lastId > 0) {
			String sql = "select * from " + getTableName() + " r where r.id = " + lastId;
			List<T> list = super.getJdbcTemplate().query(sql, new R5RowMapper());
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public T getR5ByQs(String issue) {
		String sql = "select * from " + getTableName() + " r where r.qs = '" + issue + "'";
		List<T> list = super.getJdbcTemplate().query(sql, new R5RowMapper());
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public int getYl(String issue, String code) {
		T r5 = this.getR5ByQs(issue);
		if (r5 != null) {
			String sql = "select r.id from " + getTableName() + " r where r.id < ? and r.ns = ? order by r.id desc limit 1";
			SqlRowSet rowSet = super.getJdbcTemplate().queryForRowSet(sql, new Object[] {r5.getId(), code});
			if (rowSet.next()) {
				return r5.getId() - rowSet.getInt(1) - 1;
			}
		}
		return -1;
	}

	@Override
	public int getCurrentYl(String code) {
		T r5 = this.getLastR5();
		if (r5 != null) {
			String sql = "select r.id from " + getTableName() + " r where r.id < ? and r.ns = ? order by r.id desc limit 1";
			SqlRowSet rowSet = super.getJdbcTemplate().queryForRowSet(sql, new Object[] {r5.getId() + 1, code});
			if (rowSet.next()) {
				return r5.getId() - rowSet.getInt(1);
			}
		}
		return -1;
	}
	
	@Override
	public List<T> query(String fromDate, String toDate) {
		String sql = "select * from " + getTableName() + " r where r.qs >= ? and r.qs <= ?";
		fromDate = fromDate.replaceAll("-", "");
		toDate = toDate.replaceAll("-", "");
		Object[] params = new Object[] {fromDate + "01", toDate + "99"};
		return super.getJdbcTemplate().query(sql, params, new R5RowMapper());
	}
	
	@Override
	public List<T> queryLatest(int num) {
		String sql = "select * from (select * from " + getTableName() + " r order by r.id desc limit 0,?) t order by t.id";
		return super.getJdbcTemplate().query(sql, new Object[] {num}, new R5RowMapper());
	}
	
	private class R5RowMapper implements RowMapper<T> {

		@Override
		public T mapRow(ResultSet rs, int rowNum) throws SQLException {
			T r5 = null;
			try {
				r5 = getEntityClass().newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
			r5.setId(rs.getInt("id"));
			r5.setQs(rs.getString("qs"));
			r5.setN1(rs.getInt("n1"));
			r5.setN2(rs.getInt("n2"));
			r5.setN3(rs.getInt("n3"));
			r5.setN4(rs.getInt("n4"));
			r5.setN5(rs.getInt("n5"));
			r5.setNs(rs.getString("ns"));
			r5.setJs(rs.getInt("js"));
			r5.setOs(rs.getInt("os"));
			r5.setDs(rs.getInt("ds"));
			r5.setXs(rs.getInt("xs"));
			r5.setY0(rs.getInt("y0"));
			r5.setY1(rs.getInt("y1"));
			r5.setY2(rs.getInt("y2"));
			r5.setLx(rs.getInt("lx"));
			r5.setHz(rs.getInt("hz"));
			r5.setKd(rs.getInt("kd"));
			r5.setYl(rs.getInt("yl"));
			r5.setCm(rs.getInt("cm"));
			return r5;
		}
		
	}
	
	protected abstract Class<T> getEntityClass();
	
	protected abstract String getTableName();

}
